import FingerprintJS from '@fingerprintjs/fingerprintjs';
import {userUuidInfo} from "@/api/user.ts"
import { RouteLocationNormalized } from "vue-router";
// 获取 Canvas ID 的函数
export async function getCanvasId(route: RouteLocationNormalized): Promise<void> {
    try {
        // 尝试从本地存储中获取
        const storageOmniUserUuid = localStorage.getItem('omni_user_uuid');
        if (!storageOmniUserUuid) {
            // 如果本地存储中不存在 Canvas ID，则生成新的 Canvas ID
            const fp = await FingerprintJS.load();
            const result = await fp.get();
            const canvasId: string = result.visitorId;
            await getOmniUserUuid(canvasId)
        }
        const omniUserUuid = route.query.omni_user_uuid as string | undefined;
        if(omniUserUuid){
            localStorage.setItem('omni_user_uuid', omniUserUuid);
        }
    } catch (error) {
        console.error('生成 Canvas ID 失败:', error);
    }
}


const getOmniUserUuid = async (canvasId:string)=>{
    try {
       await userUuidInfo({
            identity_type:"canvas_id",
            identity_value:canvasId
        }).then((response) => {
            const { omni_user_uuid} = response.data
            if(omni_user_uuid){
                localStorage.setItem('omni_user_uuid', omni_user_uuid);
                localStorage.setItem('identity_type', 'canvas_id');
                localStorage.setItem('identity_value', canvasId);
            }
        })
    } catch (e) {
        console.log(e);
    } finally {

    }
}